package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.terracotta.quartz.wrappers.FiredTrigger;
import org.w3c.dom.css.ElementCSSInlineStyle;

import com.mchange.util.AssertException;
import com.ray.bean.CompanyMaterialTableBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.WarehouseInOutDetailBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.pojo.PurchaseOrderDetailPojo;
import com.ray.service.CompanyMaterialService;
import com.ray.service.OrderService;
import com.ray.service.OtherWarehouseService;
import com.ray.service.WarehouseInSumMoneyService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/companyMaterial")
public class CompanyMaterialController {
	
	@Autowired
	CompanyMaterialService companyMaterialService;
	@Autowired
	OtherWarehouseService otherWarehouseService;
	@Autowired
	OrderService orderService;
	@Autowired
	WarehouseInSumMoneyService warehouseInSumMoneyService;
	
	//获取所有的供应商
	@RequestMapping("/getProviderName")
	@ResponseBody
	public Map getProviderName(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getProviderName(map));
	}
	
	//获取供应商下采购人员
	@RequestMapping("/getPurchasePeople")
	@ResponseBody
	public Map getPurchasePeople(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getPurchasePeople(map));
	}
	
	//获取采购订单id
	@RequestMapping("/getPurchaseId")
	@ResponseBody
	public Map getPurchaseId(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getPurchaseId(map));
	}
	
	//获取物料信息   根据物料类型和供应商名称
	@RequestMapping("/getMaterialDate")
	@ResponseBody
	public DatatablesViewPageBean<CompanyMaterialTableBean> getMaterialDate(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return companyMaterialService.getMaterialDate(map);
	}
	
	
	//保存其他仓库(报废仓/待处理仓)的入库单
	@RequestMapping("/saveInOtherWarehouse")
	@ResponseBody
	public Map saveInOtherWarehouse(HttpServletRequest request) {
		Map map = RequestUtil.getRequestAllParam(request);
		String message = companyMaterialService.saveInOtherWarehouse(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS+"  "+message);
	}
	
	//无采购单入库
	@RequestMapping("/savdInWarehouseNotPurchase")
	@ResponseBody
	public Map savdInWarehouseNotPurchase(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		String message = companyMaterialService.savdInWarehouseNotPurchase(map,request);
		return ResponseUtil.ResponseResult(ReturnStringUtil.SAVE_SUCCESS +"  "+message);
	}
	
	//根据供应商查询物料类型
	@RequestMapping("/getMaterialTypeByProviderName")
	@ResponseBody
	public Map getMaterialTypeByProviderName(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getMaterialTypeByProviderName(map));
	}
	
	
	//id集合查找物料信息/查找选中的无物料信息
	@SuppressWarnings("rawtypes")
	@RequestMapping("/getCheckMaterialTable")
	@ResponseBody
	public Map getCheckMaterialTable(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getCheckMaterialTable(map));
	}
	
	/**
	 * 获取订单编号用于采购单入库
	* @date 2018年4月18日 下午6:57:37 
	* @author 李斌
	* @return Map 
	* @throws 
	 */ 
	@RequestMapping("/getOrderIdOnWarehouseIn")
	@ResponseBody
	public Map getOrderIdOnWarehouseIn(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getOrderIdOnWarehouseIn(map)); 
	}
	
	/**
	 * @throws ServiceException 
	 * 获取工厂型号用于采购单入库
	* @date 2018年4月18日 下午7:30:38 
	* @author 李斌
	* @return Map 
	* @throws
	 */
	@RequestMapping("/getFactoryNoOnWarehouseIn")
	@ResponseBody
	public Map getFactoryNoOnWarehouseIn(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getFactoryNoOnWarehouseIn(map));
	}
	/**
	 * 获取客户型号用于采购单入库
	* @date 2018年4月18日 下午7:43:09 
	* @author 李斌
	* @return Map 
	* @throws
	 */ 
	@RequestMapping("/getArticleNoOnWarehouseIn")
	@ResponseBody
	public Map getArticleNoOnWarehouseIn(HttpServletRequest request) throws ServiceException {
		Map map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getArticleNoOnWarehouseIn(map));
	}
	
	
	
	/**
	 * 上传文件添加物料单 at 2018-03-29 by 李斌
	 * @throws ServiceException 
	 * @throws IOException 
	 */
	@RequestMapping("/addWarehouseByFile") 
	@ResponseBody 
	public Map addWarehouseByFile(MultipartFile filename,HttpServletRequest request) throws ServiceException, IOException{
		MyUser myUser = RequestUtil.getLoginUser(request);  
		return ResponseUtil.ResponseResult(companyMaterialService.addWarehouseByFile(filename,myUser));  
	} 
	
	//模糊查询公司物料
	@RequestMapping("/getVagueCompanyMaterialByMap") 
	@ResponseBody 
	public Map getVagueCompanyMaterialByMap(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getVagueCompanyMaterialByMap(map));  
	}
	
	//准确查询
	@RequestMapping("/getCompanyMaterialByMap") 
	@ResponseBody 
	public Map getCompanyMaterialByMap(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getCompanyMaterialByMap(map));  
	}
	
	//获取物料信息   根据物料类型和供应商名称
	@RequestMapping("/getVagueCompanyMaterialByMapToPage")
	@ResponseBody
	public DatatablesViewPageBean<CompanyMaterialTableBean> getVagueCompanyMaterialByMapToPage(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return companyMaterialService.getVagueCompanyMaterialByMapToPage(map);
	}
	
	//通过物料代码获取出入库明细
	@RequestMapping("/getWarehouseInOutByMaterialCode")
	@ResponseBody
	public Map<String,Object> getWarehouseInOutByMaterialCode(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getWarehouseInOutByMaterialCode(map));
	} 
	 
	@RequestMapping("/getOtherWarehouseMateiral")
	@ResponseBody
	public Map<String,Object> getOtherWarehouseMateiral(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getOtherWarehouseMateiral(map));
	}
	
	/**
	 * 获取成品仓数据
	 * @param orderId(订单编号)
	 * @return
	 * @author nate
	 * @throws ServiceException
	 */
	@RequestMapping("/getFinishStockCount")
	@ResponseBody
	public Map getFinishStockCount(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(otherWarehouseService.getFinishStockCount(map));
	}
	
	
	/**
	 * 成品仓出库
	 * @param orderId(实际出库订单编号)
	 * @param type(类型 1 本订单  2 库存单)
	 * @param otherOrderId(其他订单编号  当类型不为1的时候有)
	 * @param rowIndexs(行号)
	 * @param remark(备注)
	 * @param outCount(出库数)
	 * @author nate
	 * @throws ServiceException
	 * @throws CloneNotSupportedException 
	 */
	@RequestMapping("/finishStockOut")
	@ResponseBody
	public Map finishStockOut(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		String message = otherWarehouseService.finishStockOut(map,request);
		return ResponseUtil.ResponseResult(ReturnStringUtil.OPERATION_SUCCESS+(message==null?"":message));
	}
	
	/**
	 * 成品仓出库
		otherOrderId: SC2018100600152
		outCount: 1
		customerName: 124
		count: 1
		inOutMoney: 1
		inOutUnit: 付
		inCountPrice: 1
		orderId: SC2018100600152
		type: 1
		rowIndexs: 1
		remark: 
		isAduit: false
	 * jsonArray 
	 * @author nate
	 * @throws ServiceException
	 * @throws CloneNotSupportedException 
	 */
	@RequestMapping("/finishStockOutAll")
	@ResponseBody
	public Map finishStockOutAll(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		Assert.isTrue(map.get("jsonArray")!=null && !"".equals(map.get("jsonArray")),"未获取到参数");
		JSONArray jsonArray=JSONArray.fromObject(map.get("jsonArray").toString());
		for(int i=0;i<jsonArray.size();i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			map.put("otherOrderId", jsonObject.get("otherOrderId"));
			map.put("outCount", jsonObject.get("outCount"));
			map.put("customerName", jsonObject.get("customerName"));
			map.put("count", jsonObject.get("count"));
			map.put("inOutMoney", jsonObject.get("inOutMoney"));
			map.put("inOutUnit", jsonObject.get("inOutUnit"));
			map.put("inCountPrice", jsonObject.get("inCountPrice"));
			map.put("orderId", jsonObject.get("orderId"));
			map.put("type", jsonObject.get("type"));
			map.put("rowIndexs", jsonObject.get("rowIndexs"));
			map.put("remark", jsonObject.get("remark"));
			map.put("isStages", jsonObject.get("isStages"));
			map.put("stagesCount", jsonObject.get("stagesCount"));
			map.put("stagesInterval", jsonObject.get("stagesInterval"));
			otherWarehouseService.finishStockOut(map,request);
		}
		return ResponseUtil.ResponseResult(ReturnStringUtil.OPERATION_SUCCESS);
	}
 
	/**
	 * 查询所有的查询条件   
	 * @param request
	 * @return map 两个键分别为物料信息和订单信息
	 * @throws ServiceException
	 */
	@RequestMapping("/getAllSearchWhere")
	@ResponseBody
	public Map getAllSearchWhere(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(otherWarehouseService.getAllSearchWhere(map));
	}
	
	/**
	 * 查询所有的订单查询条件   
	 * @param request
	 * @return map 两个键分别为物料信息和订单信息
	 * @throws ServiceException
	 */
	@RequestMapping("/getAllOrderSearchWhere")
	@ResponseBody
	public Map getAllOrderSearchWhere(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(otherWarehouseService.getAllOrderSearchWhere(map));
	}
	
	
	
	/**
	 * 查询所有的查询条件   四要素+仓库
	 * @param type(仓库  1 成品  2 报废 3 待处理 4 原料仓)
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/getAllOutData")
	@ResponseBody
	public DatatablesViewPageBean getAllOutData(HttpServletRequest request,int type) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return otherWarehouseService.getAllOutData(map,type);
	}
	
	/**
	 * 物料出库
	 * @param request
	 * @param materialArray(参数字符串数组)
	 * @param materialCode(物料代码)
	 * @param customerName(客户名称)
	 * @param inOutUnit(出库单位)
	 * @param inCountPrice(出库单价)
	 * @param count(出库数量)
	 * @param remark(备注)
	 * @param warehouseType(仓库类型)
	 * @return
	 * @throws ServiceException
	 * @throws CloneNotSupportedException 
	 */
	@RequestMapping("/materialOutToCustomer")
	@ResponseBody
	public Map materialOutToCustomer(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		String message = otherWarehouseService.materialOutToCustomer(map,request);
		return ResponseUtil.ResponseResult(ReturnStringUtil.OUT_SUCCESS+(message==null?"":message));
	}
	
	
	/**
	 * @param orderId(订单编号)
	 * @param rowIndexs(行号)
	 * @param request
	 * @return
	 */
	@RequestMapping("/getOrderFinishOutDetail")
	@ResponseBody
	public Map getOrderFinishOutDetail(HttpServletRequest request){
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(otherWarehouseService.getOrderFinishOutDetail(map));
	}
	
	/**
	 * 通过订单编号和行号获取订单信息
	* @date 2018年7月25日 下午2:52:28 
	* @author 李斌
	* @return List<OrderInfoDetailBean> 
	* @throws
	 */
	@RequestMapping("/getMaterialOkDetailByOrder")
	@ResponseBody
	public Map<String,Object> getMaterialOkDetailByOrder(HttpServletRequest request){
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getMaterialOkDetailByOrder(map));
	}
	
	//获取所有的供应商和商城入驻的公司
	@RequestMapping("/getProviderNameAndShoppingCallCompany")
	@ResponseBody
	public Map getProviderNameAndShoppingCallCompany(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(companyMaterialService.getProviderNameAndShoppingCallCompany(map));
	}
}

